#include <stdlib.h>

#include "InventoryScreen.h"
#include "../Main.h"
#include "../Game.h"

InventoryScreen::InventoryScreen(SDL_Surface* screen, TTF_Font* font, std::map<std::string, SDL_Rect>& blits) : blits(blits) {
	this->screen = screen;
	this->width = screen->w;
	this->height = screen->h;

	this->inventorySurface = 0;
	this->interfaceSurface = 0;
	this->smallPotionSurface = 0;
	this->bigPotionSurface = 0;
	this->font = font;
	this->downButton = b_none;

	this->weaponText = 0;
	this->armorText = 0;
	this->smallPotionText = 0;
	this->bigPotionText = 0;
}

InventoryScreen::~InventoryScreen() {
	if (inventorySurface)
		SDL_FreeSurface(inventorySurface);

	if (interfaceSurface)
		SDL_FreeSurface(interfaceSurface);

	if (smallPotionSurface)
		SDL_FreeSurface(smallPotionSurface);

	if (bigPotionSurface)
		SDL_FreeSurface(bigPotionSurface);

	if (weaponText)
		SDL_FreeSurface(weaponText);

	if (armorText)
		SDL_FreeSurface(armorText);

	if (smallPotionText)
		SDL_FreeSurface(smallPotionText);

	if (bigPotionText)
		SDL_FreeSurface(bigPotionText);
}

InventoryScreen::Button InventoryScreen::isInsideButton(int x, int y) {
	if (isPointInsideRect(x, y, 293, 193, 34, 34)) { //broń w górę
		return b_weaponUp;
	} else if (isPointInsideRect(x, y, 293, 228, 34, 34)) { //broń w dół
		return b_weaponDown;
	} else if (isPointInsideRect(x, y, 293, 393, 34, 34)) { //pancerz w górę
		return b_armorUp;
	} else if (isPointInsideRect(x, y, 293, 428, 34, 34)) { //pancerz w dół
		return b_armorDown;
	} else if (isPointInsideRect(x, y, 610, 190, 72, 610)) { //użyj dużego potiona
		return b_useSmallPotion;
	} else if (isPointInsideRect(x, y, 610, 390, 72, 610)) { //użyj małego potiona
		return b_useBigPotion;
	} else {
		return b_none;
	}
}

bool InventoryScreen::Draw() {
	if (!inventorySurface) {
		if (!(inventorySurface = IMG_Load("img/inventory.png"))) {
			return DebugMessage("Inventory", "Draw", "Couldn't load inventory interface file!", error);
		}
	}

	if (!interfaceSurface) {
		if (!(interfaceSurface = IMG_Load("img/interface.png"))) {
			return DebugMessage("Interface", "Draw", "Couldn't load interface file!", error);
		}
	}

	if (!smallPotionSurface) {
		if (!(smallPotionSurface = LoadBMP("img/inventory/small-potion.bmp", 0xFF, 0, 0xFF))) {
			return DebugMessage("Interface", "Draw", "Couldn't load small potions file!", error);
		}
	}

	if (!bigPotionSurface) {
		if (!(bigPotionSurface = LoadBMP("img/inventory/big-potion.bmp", 0xFF, 0, 0xFF))) {
			return DebugMessage("Interface", "Draw", "Couldn't load big potions file!", error);
		}
	}

	if (!weaponText) {
		if (!(weaponText = PrepareText("Weapon: ", font, 0xCC, 0xCC, 0xCC))) {
			return DebugMessage("Interface", "Draw", "Couldn't create weapon text!", error);
		}
	}

	if (!armorText) {
		if (!(armorText = PrepareText("Armor: ", font, 0xCC, 0xCC, 0xCC))) {
			return DebugMessage("Interface", "Draw", "Couldn't create armor text!", error);
		}
	}

	if (!smallPotionText) {
		if (!(smallPotionText = PrepareText("Small potion: ", font, 0xCC, 0xCC, 0xCC))) {
			return DebugMessage("Interface", "Draw", "Couldn't create small potion text!", error);
		}
	}

	if (!bigPotionText) {
		if (!(bigPotionText = PrepareText("Big potion: ", font, 0xCC, 0xCC, 0xCC))) {
			return DebugMessage("Interface", "Draw", "Couldn't create big potion text!", error);
		}
	}

	//tło
	DisplaySurface(inventorySurface, 50, 50, 0, 0, 920, 550);

	//broń
	Character* player = Game::GetInstance().interface->getWorld()->player;
	DisplaySurface(weaponText, 200, 150);
	DisplaySurface(interfaceSurface, 220, 190, blits["button"]);
	DisplaySurface(player->activeWeapon->image, 240, 210);
	WriteText(player->activeWeapon->name.c_str(), screen, font, 325, 195, 0xCC, 0xCC, 0xCC);
	string text = "Attack: ";
	text.insert(text.size(), 1, player->activeWeapon->attack + '0');
	WriteText(text.c_str(), screen, font, 330, 220, 0xCC, 0xCC, 0xCC);

	if (downButton == b_weaponUp)
		DisplaySurface(interfaceSurface, 293, 193, blits["pressedUp"]);
	else
		DisplaySurface(interfaceSurface, 293, 193, blits["up"]);
	if (downButton == b_weaponDown)
		DisplaySurface(interfaceSurface, 293, 228, blits["pressedDown"]);
	else
		DisplaySurface(interfaceSurface, 293, 228, blits["down"]);

	//pancerz
	DisplaySurface(armorText, 200, 350);
	DisplaySurface(interfaceSurface, 220, 390, blits["button"]);
	DisplaySurface(player->activeArmor->image, 240, 410);
	WriteText(player->activeArmor->name.c_str(), screen, font, 325, 395, 0xCC, 0xCC, 0xCC);
	text = "Armor: ";
	text.insert(text.size(), 1, player->activeArmor->defense + '0');
	WriteText(text.c_str(), screen, font, 330, 420, 0xCC, 0xCC, 0xCC);

	if (downButton == b_armorUp)
		DisplaySurface(interfaceSurface, 293, 393, blits["pressedUp"]);
	else
		DisplaySurface(interfaceSurface, 293, 393, blits["up"]);
	if (downButton == b_armorDown)
		DisplaySurface(interfaceSurface, 293, 428, blits["pressedDown"]);
	else
		DisplaySurface(interfaceSurface, 293, 428, blits["down"]);

	//small potion
	DisplaySurface(smallPotionText, 600, 150);
	DisplaySurface(interfaceSurface, 610, 190, blits["button"]);
	if (downButton == b_useSmallPotion)
		DisplaySurface(interfaceSurface, 610, 190, blits["pressedButtonFrame"]);
	DisplaySurface(smallPotionSurface, 630, 210);
	WriteText("Power: 5", screen, font, 695, 190, 0xCC, 0xCC, 0xCC);
	text = "Number: ";
	text.insert(text.size(), 1, player->smallPotionsNumber + '0');
	WriteText(text.c_str(), screen, font, 695, 220, 0xCC, 0xCC, 0xCC);

	//big potion
	DisplaySurface(bigPotionText, 600, 350);
	DisplaySurface(interfaceSurface, 610, 390, blits["button"]);
	if (downButton == b_useBigPotion)
		DisplaySurface(interfaceSurface, 610, 390, blits["pressedButtonFrame"]);
	DisplaySurface(bigPotionSurface, 630, 410);
	WriteText("Power: ", screen, font, 695, 390, 0xCC, 0xCC, 0xCC);
	text = "Number: ";
	text.insert(text.size(), 1, player->bigPotionsNumber + '0');
	WriteText(text.c_str(), screen, font, 695, 420, 0xCC, 0xCC, 0xCC);
	return true;
}

void InventoryScreen::OnMouseButtonDown(int x, int y, int button) {
	if (button == SDL_BUTTON_LEFT) {
		printf("%d, %d\n", x, y);
		downButton = isInsideButton(x, y);
	}
}

void InventoryScreen::OnMouseButtonUp(int x, int y, int button) {
	if (button == SDL_BUTTON_LEFT) {
		Button upButton = isInsideButton(x, y);

		Draw();
		if (downButton != upButton) {
			upButton = b_none;
		} else {
			Game& game = Game::GetInstance();
			SDL_Event event;
			event.type = SDL_USEREVENT;
			Character* player = Game::GetInstance().interface->getWorld()->player;
			switch (downButton) {
				case b_weaponUp: {
					if (player->activeWeapon != player->weapons.begin()) {
						--player->activeWeapon;
						event.user.code = e_weaponChanged;
						SDL_PushEvent(&event);
					}
					break;
				} case b_weaponDown: {
					if (player->activeWeapon != --player->weapons.end()) {
						++player->activeWeapon;
						event.user.code = e_weaponChanged;
						SDL_PushEvent(&event);
					}
					break;
				} case b_armorUp: {
					if (player->activeArmor != player->armors.begin()) {
						--player->activeArmor;
						event.user.code = e_armorChanged;
						SDL_PushEvent(&event);
					}
					break;
				} case b_armorDown: {
					if (player->activeArmor != --player->armors.end()) {
						++player->activeArmor;
						event.user.code = e_armorChanged;
						SDL_PushEvent(&event);
					}
					break;
				} case b_useSmallPotion: {
					player->useSmallPotion();
					break;
				} case b_useBigPotion: {
					player->useBigPotion();
					break;
				}
			}
			downButton = b_none;
		}
	}
}
